//
//  main.m
//  05-HomeWork
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
//    一、
//    
//    1.创建1个包含5个字符串元素的数组；
//    2.用for循环倒序输出所有元素；
//        3.用for...in完成所有字符串元素的拼接(生成一个
//            新的字符串)，并输出拼接好的字符串。
#if 0
    NSArray *arr = @[@"iPhone", @"iPad", @"iMac", @"iPod", @"MacPro"];

    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        NSLog(@"%@", arr[i]);
    }
    NSMutableString *string = [NSMutableString string];
    for (NSString *str in arr) {
         [string appendString:str];
    }
    NSLog(@"%@", string);
#endif
#if 0
//    二、
//    
//    1.创建1个包含5个person对象的数组(姓名用英文)；
//    2.用for循环找到年龄最大的Person对象；
//        3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [[Person alloc] init];
    per1.name = @"HaoLinLi";
    per1.age = 23;
    per1.sex = @"nan";
    Person *per2 = [[Person alloc] init];
    per2.name = @"TanQiLiu";
    per2.age = 22;
    per2.sex = @"nan";
    Person *per3 = [[Person alloc] init];
    per3.name = @"BaoGuo";
    per3.age = 21;
    per3.sex = @"nan";
    Person *per4 = [[Person alloc] init];
    per4.name = @"HanYu";
    per4.age = 25;
    per4.sex = @"nan";
    Person *per5 = [[Person alloc] init];
    per5.name = @"PengfeiLuo";
    per5.age = 18;
    per5.sex = @"nan";
    NSArray *personArray = @[per1, per2, per3, per4, per5];
    Person *maxAgePerson = [personArray objectAtIndex:0];
    for (NSInteger i = 0; i < personArray.count; i++) {
        if ([personArray[i] age] > [maxAgePerson age]) {
            maxAgePerson = personArray[i];
        }
    }
#if 0
    NSLog(@"%@", maxAgePerson);
    for (Person *per in personArray) {
        if (per.age > 20) {
         per.name = [per.name stringByAppendingString:@"-brother"];
        }
    }
    NSLog(@"%@", personArray);
#endif
//    三、
//    
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
    NSMutableArray *newPersonArray = [NSMutableArray arrayWithArray:personArray];
    [newPersonArray sortUsingSelector:@selector(compareWithName:)];
    NSLog(@"%@", newPersonArray);
    [newPersonArray sortUsingSelector:@selector(compareWithAge:)];
    NSLog(@"%@", newPersonArray);
    [newPersonArray sortUsingSelector:@selector(compareWithSex:)];
    NSLog(@"%@", newPersonArray);
#endif

//    四、
//    
//    处理省市区文件中的area.txt
//    将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
#if 0
    
//    1>
   //获取文件字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
  //将文件字符串通过\n切割
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
    // 遍历出的是省名
        if (![string hasPrefix:@" "]) {
//             NSLog(@"省");
    // 创建省字典
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
   // 省字典中有省名
            [provinceDic setObject:string forKey:@"provinceName"];
   // 创建市数组
            NSMutableArray *cityArray = [NSMutableArray array];
   // 省字典中有市数组
            [provinceDic setObject:cityArray forKey:@"cityArray"];
   // 将省字典添加到allProvinceArray数组中
            [allProvinceArray addObject:provinceDic];
  //  遍历的是市名
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"   "]) {
//            NSLog(@"市");
    // 从allProvinceArray 取出省字典（通过lastObject）
       NSMutableDictionary *provinceDic = [allProvinceArray lastObject];
     // 从provinceDic取出市数组
        NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
    // 创建市字典
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:string forKey:@"cityName"];
   // 创建区数组
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
   // 将市字典添加到市数组中
            [cityArray addObject:cityDic];
   //  遍历出的是区
        } else {
//            NSLog(@"区");
     // 取出省
            NSMutableDictionary *province = [allProvinceArray lastObject];
    // 取出市数组
            NSMutableArray *cityArray = [province objectForKey:@"cityArray"];
     // 取出市
            NSMutableDictionary *cityDic = [cityArray lastObject];
     // 取出区数组
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
   // 将区存入区数组中
            [zoneArray addObject:string];
        }
    }
    NSLog(@"%@", allProvinceArray);
#endif
#if 0
    //获取文件字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    //将文件字符串通过\n切割
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
//        NSLog(@"%@", string);
        if (![string hasPrefix:@" "]) {
      // 省
      // 创建省的对象
            Province *province = [Province zoneWithName:string];
      // 将省的对象添加到allProvinceArray数组中
            [allProvinceArray addObject:province];
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
      //  市
      // 创建市对象
            City *city = [City zoneWithName:string];
       // 从allProvinceArray 通过lastObject找到省对象
            Province *province = [allProvinceArray lastObject];
            //将市对象添加到省对象的市数组中
            [province.array addObject:city];
//            NSLog(@"%@",province.array);
        } else {
      // 区
      // 创建区对象
            Zone *zone = [Zone zoneWithName:string];
            Province *province = [allProvinceArray lastObject];
            City *city = [province.array lastObject];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", allProvinceArray);
    
#endif


//    五、拓展
//    
//    上述作业做完, 处理Location1.txt
//    要求同第四题。自己寻找规律
#if 0
   
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@","];
    NSArray *cityArray = [NSMutableArray array];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
     cityArray = [string componentsSeparatedByString:@"|"];
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[cityArray firstObject], @"cityName", [cityArray lastObject], @"cityNumber", nil];
        [allCityDicArray addObject:cityDic];
    }
    // 排序（根据cityDic的cityNumber升序排序）
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    for (NSMutableDictionary *dictionary in allCityDicArray) {
        if ([dictionary[@"cityNumber"] hasSuffix:@"0000"]) {
      //  省
            NSMutableArray *cityArray = [NSMutableArray array];
            [dictionary setObject:cityArray forKey:@"cityArray"];
            [allProvinceArray addObject:dictionary];
        } else if ([dictionary[@"cityNumber"] hasSuffix:@"00"] && ![dictionary[@"cityNumber"] hasSuffix:@"0000"]) {
     // 市
            NSMutableArray *zoneArray = [NSMutableArray array];
            [dictionary setObject:zoneArray forKey:@"zoneArray"];
    // 把市字典放倒cityArray
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            NSMutableArray *cityArray = proDic[@"cityArray"];
            [cityArray addObject:dictionary];
        } else {
    // 区
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            NSMutableArray *cityArray = proDic[@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *zoneArray = cityDic[@"zoneArray"];
            [zoneArray addObject:dictionary];
        }
    }
    NSLog(@"%@", allProvinceArray);
#endif    
    
    
    
#if 1
    NSString *sourceString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS10710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@","];
    NSArray *cityArray = [NSMutableArray array];
    NSMutableArray *tempCityArray = [NSMutableArray array];
    for (NSString *string in sourceArray) {
        cityArray = [string componentsSeparatedByString:@"|"];
        TempCity *tempCity = [TempCity tempCityWithName:[cityArray firstObject] number:[cityArray lastObject]];
        [tempCityArray addObject:tempCity];
    }
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    [tempCityArray sortUsingDescriptors:@[sort]];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (TempCity *tempCity in tempCityArray) {
        if ([tempCity.number hasSuffix:@"0000"]) {
             //  省
        Province *province = [Province zoneWithTempCity:tempCity];
        [allProvinceArray addObject:province];
        } else if ([tempCity.number hasSuffix:@"00"] && ![tempCity.number hasSuffix:@"0000"]) {
            // 市
            Province *province = [allProvinceArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [province.array addObject:city];
        }else {
           // 区
            Province *province = [allProvinceArray lastObject];
            City *city = [province.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        }
    
    }
    NSLog(@"%@", allProvinceArray);
#endif
    return 0;
}
